#include "sdl_gamepad.h"

SDL_GAMEPAD::SDL_GAMEPAD(QObject *parent = 0):QThread(parent)
{

    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_JoystickEventState(SDL_ENABLE);
    joystick = SDL_JoystickOpen(0);

    axis_activated = false;
    axis_left_act = false;
    axis_right_act = false;

}

SDL_GAMEPAD::~SDL_GAMEPAD()
{
    SDL_JoystickClose(joystick);
    SDL_Quit();
    killTimer(timer_id);
}

void SDL_GAMEPAD::run()
{
    timer_id=startTimer(5);
}

void SDL_GAMEPAD::timerEvent(QTimerEvent *)
{
    while( SDL_PollEvent( &event ) ){

        if((SDL_JoystickGetAxis(joystick, 0) == 0 && axis_activated) || (SDL_JoystickGetAxis(joystick, 1) == 0 && axis_activated)){

            if(axis_left_act){
                emit signal_gamepad_released(J_LEFT);
                axis_left_act = false;
            }

            if(axis_right_act){
                emit signal_gamepad_released(J_RIGHT);
                axis_right_act = false;
            }

            axis_activated = false;

            emit signal_gamepad_released(J_TOP);
            emit signal_gamepad_released(J_DOWN);
        }

        switch (event.type){
        case SDL_JOYAXISMOTION:{
            if(event.jaxis.axis == 0){
                if(SDL_JoystickGetAxis(joystick, 0) < 0){
                    emit signal_gamepad_clicked(J_LEFT);
                    axis_activated = true;
                    axis_left_act = true;
                }

                if(SDL_JoystickGetAxis(joystick, 0) > 0){
                    emit signal_gamepad_clicked(J_RIGHT);
                    axis_activated = true;
                    axis_right_act = true;
                }
            }

            if(event.jaxis.axis == 1){
                if(SDL_JoystickGetAxis(joystick, 1) < 0){
                    emit signal_gamepad_clicked(J_TOP);
                    axis_activated = true;
                }

                if(SDL_JoystickGetAxis(joystick, 1) > 0){
                    emit signal_gamepad_clicked(J_DOWN);
                    axis_activated = true;
                }

            }
        }break;

        case SDL_JOYBUTTONDOWN:{
            switch (event.jbutton.button) {

                case 6:
                    emit signal_gamepad_clicked(J_GATLING);
                    break;

                case 7:
                    emit signal_gamepad_clicked(J_CANNON);
                    break;

                case 0:
                    emit signal_gamepad_clicked(J_SHUTTLE);
                    break;

                case 1:
                    emit signal_gamepad_clicked(J_SPEEDBOAT);
                    break;

                case 2:
                    emit signal_gamepad_clicked(J_SUBMARINE);
                    break;

            }

        }break;

        case SDL_JOYBUTTONUP:{
            switch (event.jbutton.button) {
                case 0:
                    emit signal_gamepad_released(J_ENTER);
                    break;

                case 6:
                    emit signal_gamepad_released(J_GATLING);
                    break;

                case 7:
                    emit signal_gamepad_released(J_CANNON);
                    break;
            }
        }break;

        }
    }
}
